<% bool_to_yesno = { true => 'yes', false => 'no' } -%>
<%
  eth_opts = []

  eth_opts << "BOOTPROTO=#{@bootproto}"

  unless @delay.nil?
    eth_opts << "DELAY=#{@delay}"
  end

  eth_opts << "DEVICE=#{@name}"

  unless @dhcp_hostname.nil?
    eth_opts << "DHCP_HOSTNAME=#{@dhcp_hostname}"
  end
  unless @gateway.nil?
    eth_opts << "GATEWAY=#{@gateway}"
  end

  if !@macaddr.nil?
    eth_opts << "MACADDR=#{@macaddr}"
  elsif !@hwaddr.nil?
    eth_opts << "HWADDR=#{@hwaddr}"
    eth_opts << "# Puppet not setting HWADDR due to 'hwaddr' having a value of '#{@hwaddr}'"
  elsif !@auto_discover_mac
    # If your net type is not a non-physical interface, as per the list from the official documentation,
    # then auto discover your MAC/Hardware address.
    #
    # Handle virtual interface MAC discovery.
    lmacaddr = scope.lookupvar("::macaddress_#{@name.split(':')[0]}")

    if lmacaddr.match(/^(00:?)+$/)
      raise Puppet::ParseError.new("Found MAC address of all 0's for #{@name}")
    end

    eth_opts << "HWADDR=#{lmacaddr}"
  end

  unless @ipaddr.nil?
    eth_opts << "IPADDR=#{@ipaddr}"
  end
  unless @ipv6addr.nil?
    eth_opts << "IPV6ADDR=#{@ipv6addr}"
  end
  unless @ipv6_defaultgw.nil?
    eth_opts << "IPV6_DEFAULTGW=#{@ipv6_defaultgw}"
  end
  unless @ipv6_autoconf.nil?
    eth_opts << "IPV6_AUTOCONF=#{bool_to_yesno[@ipv6_autoconf]}"
  end
  unless @ipv6_control_radvd.nil?
    eth_opts << "IPV6_CONTROL_RADVD=#{bool_to_yesno[@ipv6_control_radvd]}"
  end
  unless @ipv6_defroute.nil?
    eth_opts << "IPV6_DEFROUTE=#{bool_to_yesno[@ipv6_defroute]}"
  end
  unless @ipv6init.nil?
    eth_opts << "IPV6INIT=#{bool_to_yesno[@ipv6init]}"
  end
  unless @ipv6_mtu.nil?
    eth_opts << "IPV6_MTU=#{@ipv6_mtu}"
  end
  unless @ipv6_privacy.nil?
    eth_opts << "IPV6_PRIVACY=#{@ipv6_privacy}"
  end
  unless @ipv6_radvd_pidfile.nil?
    eth_opts << "IPV6_RADVD_PIDFILE=#{@ipv6_radvd_pidfile}"
  end
  unless @ipv6_radvd_trigger_action.nil?
    eth_opts << "IPV6_RADVD_TRIGGER_ACTION=#{@ipv6_radvd_trigger_action}"
  end
  unless @ipv6_router.nil?
    eth_opts << "IPV6_ROUTER=#{bool_to_yesno[@ipv6_router]}"
  end
  unless @ipv6addr_secondaries.nil?
    eth_opts << "IPV6_SECONDARIES=#{@ipv6addr_secondaries.join(' ')}"
  end
  unless @ipv6to4init.nil?
    eth_opts << "IPV6TO4INIT=#{bool_to_yesno[@ipv6to4init]}"
  end
  unless @ipv6to4_ipv4addr.nil?
    eth_opts << "IPV6TO4_IPV4ADDR=#{@ipv6to4_ipv4addr}"
  end
  unless @ipv6to4_mtu.nil?
    eth_opts << "IPV6TO4_MTU=#{@ipv6to4_mtu}"
  end
  unless @ipv6to4_relay.nil?
    eth_opts << "IPV6TO4_RELAY=#{@ipv6to4_relay}"
  end
  unless @ipv6to4_routing.nil?
    eth_opts << "IPV6TO4_ROUTING=#{@ipv6to4_routing}"
  end
  unless @isalias.nil?
    eth_opts << "ISALIAS=#{bool_to_yesno[@isalias]}"
  end
  unless @master.nil?
    eth_opts << "MASTER=#{@master}"
  end
  unless @mtu.nil?
    eth_opts << "MTU=#{@mtu}"
  end
  unless @netmask.nil?
    eth_opts << "NETMASK=#{@netmask}"
  end
  if @name.include?(':')
    eth_opts << "ONPARENT=#{bool_to_yesno[@onboot]}"
  else
    eth_opts << "ONBOOT=#{bool_to_yesno[@onboot]}"
  end
  unless @peerdns.nil?
    eth_opts << "PEERDNS=#{bool_to_yesno[@peerdns]}"
  end

  if @vlan
    eth_opts << 'VLAN=yes'
    unless @vlan_name_type.nil?
      eth_opts << "VLAN_NAME_TYPE=#{@vlan_name_type}"
    end
  end

  unless @physdev.nil?
    eth_opts << "PHYSDEV=#{@physdev}"
  end
  unless @dns1.nil?
    eth_opts << "DNS1=#{@dns1}"
  end
  unless @dns2.nil?
    eth_opts << "DNS2=#{@dns2}"
  end
  unless @persistent_dhclient.nil?
    eth_opts << "PERSISTENT_DHCLIENT=#{bool_to_yesno[@persistent_dhclient]}"
  end
  unless @slave.nil?
    eth_opts << "SLAVE=#{bool_to_yesno[@slave]}"
  end
  unless @net_type.nil?
    eth_opts << "TYPE=#{@net_type}"
  end
  unless @userctl.nil?
    eth_opts << "USERCTL=#{bool_to_yesno[@userctl]}"
  end
  unless @metric.nil?
    eth_opts << "METRIC=#{@metric}"
  end
  unless @hotplug.nil?
    eth_opts << "HOTPLUG=#{bool_to_yesno[@hotplug]}"
  end
  unless @window.nil?
    eth_opts << "WINDOW=#{@window}"
  end
  unless @srcaddr.nil?
    eth_opts << "SRCADDR=#{@srcaddr}"
  end
  unless @nozeroconf.nil?
    eth_opts << "NOZEROCONF=#{@nozeroconf}"
  end
  unless @dhcprelease.nil?
    eth_opts << "DHCPRELEASE=#{@dhcprelease}"
  end
  unless @dhclient_ignore_gateway.nil?
    eth_opts << "DHCLIENT_IGNORE_GATEWAY=#{bool_to_yesno[@dhclient_ignore_gateway]}"
  end

  t_dhclientargs = []
  unless @dhclient_request_option_list.nil?
    if scope.call_function('versioncmp', [@operatingsystemmajrelease,'8']) < 0
      t_dhclientargs << "-R #{@dhclient_request_option_list.join(' ')}"
    else
      t_dhclientargs << "--request-options #{@dhclient_request_option_list.join(' ')}"
    end
  end

  unless @dhclient_timeout.nil?
    if scope.call_function('versioncmp', [@operatingsystemmajrelease,'8']) < 0
      t_dhclientargs << "-timeout #{@dhclient_timeout}"
    else
      t_dhclientargs << "--timeout #{@dhclient_timeout}"
    end
  end

  unless @dhclient_vendor_class_identifier.nil?
    t_dhclientargs << "-V #{@dhclient_vendor_class_identifier}"
  end
  unless t_dhclientargs.nil?
    eth_opts << "DHCLIENTARGS='#{t_dhclientargs.join(' ')}'"
  end
  unless @ethtool_opts.nil?
    eth_opts << "ETHTOOL_OPTS=#{@ethtool_opts.join(' ')}"
  end

  unless @reorder_hdr.nil?
    eth_opts << "REORDER_HDR=#{bool_to_yesno[@reorder_hdr]}"
  end

  unless @arp.nil?
    eth_opts << "ARP=#{bool_to_yesno[@arp]}"
  end

  unless @linkdelay.nil?
    eth_opts << "LINKDELAY=#{@linkdelay}"
  end
  unless @bridge.nil?
    eth_opts << "BRIDGE=#{@bridge}"
  end
  unless @nm_controlled.nil?
    eth_opts << "NM_CONTROLLED=#{bool_to_yesno[@nm_controlled]}"
  end

  # Take care of bonding
  require 'ipaddr'

  t_bonding_options = []

  unless @bond_arp_interval.nil?
    t_bonding_options << "arp_interval=#{@bond_arp_interval}"

    if ( !@bond_arp_ip_target.nil? ) && ( @bond_arp_interval > 0 )
      begin
        IPAddr.new(@bond_arp_ip_target)
      rescue
        raise Puppet::ParseError.new("bond_arp_ip_target must be a valid IP address")
      end
      t_bonding_options << "arp_ip_target=#{@bond_arp_ip_target}"
    end
  end

  unless @bond_lacp_rate.nil?
    t_bonding_options << "#lacp_rate=#{@bond_lacp_rate}"
  end

  unless @bond_max_bonds.nil?
    t_bonding_options << "max_bonds=#{@bond_max_bonds}"
  end

  unless @bond_miimon.nil?
      t_bonding_options << "miimon=#{@bond_miimon}"

    # These two are only valid if miimon is being used.
    unless @bond_downdelay.nil?
      t_bonding_options << "downdelay=#{@bond_downdelay}"
    end

    unless @bond_updelay.nil?
      t_bonding_options << "updelay=#{@bond_updelay}"
    end
  end

  unless @bond_mode.nil?
      t_bonding_options << "mode=#{@bond_mode}"

    if ( !@bond_primary.nil? ) && ( @bond_mode.eql?("active-backup") || @bond_mode.eql?("1") )
      t_bonding_options << "primary=#{@bond_primary}"
    end
  end

  unless @bond_use_carrier.nil?
    t_bonding_options << "bond_use_carrier=#{@bond_use_carrier}"
  end

  unless @bond_xmit_hash_policy.nil?
    t_bonding_options << "xmit_hash_policy=#{@bond_xmit_hash_policy}"
  end

  unless t_bonding_options.empty?
    eth_opts << "BONDING_OPTS='#{t_bonding_options.join(' ')}'"
  end

  unless @autoconnect_slaves.nil?
    eth_opts << "AUTOCONNECT_SLAVES=#{bool_to_yesno[@autoconnect_slaves]}"
  end
-%>
<%= eth_opts.join("\n") %>
